package com.airspyar;


public class FlightState {
	private final static int MILLIS_IN_HOUR=3600*1000;
	private final static int DATA_DELAY=1000*4;
	private final SimpleLocation location;
	private final String callsign;
	private final String hex;
	private final String reg;
	private final int track;
	private final float distance;
	private final Vector3d distanceVector;
	private final int speed;
	private final long time;
	
	public FlightState(FlightState state){
		this(state.location, state.callsign, state.reg, state.hex, state.speed, state.track, state.distanceVector, state.time);
	}
	
	public FlightState(SimpleLocation location, String callsign, String reg, String hex, int speed, int track, Vector3d distanceVector, long time){
		this.location=location;
		this.callsign=callsign;
		this.reg=reg;
		this.hex=hex;
		this.track=track;
		this.distance=distanceVector.lenght();
		this.distanceVector=distanceVector;
		this.speed=speed;
		this.time=time;
	}
	
	public boolean isTheSameFlight(FlightState otherState){
		if(otherState==null)
			return false;
		
		return callsign.equalsIgnoreCase(otherState.callsign);
	}
	
	public Vector3d getUpdatedDistVector(){
		int dt=DATA_DELAY;
		if(this.time!=0)
			dt+=(int)(SynchronizedClock.currentTimeMillis()-this.time);

		float dislocation=this.speed*((float)dt/MILLIS_IN_HOUR);
		double track=Math.toRadians(this.track);
		float dx=(float)Math.sin(track)*dislocation;
		float dy=(float)Math.cos(track)*dislocation;
		
		Vector3d updatedDistVector=this.distanceVector.clone();
		updatedDistVector.add(dx, dy, 0);
		
		return updatedDistVector;
	}
	
	public SimpleLocation getLocation(){
		return location;
	}
	
	public String getCallsign(){
		return callsign;
	}

	public String getReg(){
		return reg;
	}
	
	public String getHex(){
		return hex;
	}
	
	public int getSpeed(){
		return speed;
	}	
	
	public int getTrack(){
		return track;
	}
	
	public float getDistance(){
		return distance;
	}
	
	public Vector3d getDistanceVector(){
		return distanceVector;
	}
	
	public long getTime(){
		return time;
	}
}
